﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
namespace BIN
{
    public enum CONFIG_KEY
    {
        UPDATE_KEY, //升级关键字,对应AlertInfo.xml的LimitKey
        IS_UPDATE_AGENT, //是否自动升级代理
        AGENT_UPDATE_INTERVAL ,//处动升级代理的升级间隔时间(M)
        SHARE_FOLDER, //自动升级代理的共享目录
        FIXXED_UPDATE_URL, //固定的升级地址(UpdateSilent)
        ONLY_KEEP_ME  //升级完之后只保留<Url>
    }
    /**********************
     * author :bin
     * 保存配置文件
     * ********************/
    public class clsConfig
    {
        private string _ConfigFile = "";
        private DataTable DtConfig
        {
            get { return _DS.Tables["Section"]; }
        }
        private DataSet _DS = null;
        /// <summary>
        /// 取配置文件的完全路径
        /// </summary>
        protected virtual string ConfigFilePath()
        {
            return (ShareMet.GetAppDir() + @"UserConfig.xml");
        }
        public clsConfig()
        {
            _ConfigFile = ConfigFilePath();
            CreateDataBase();
        }
        public string this[string SectionKey]
        {
            get
            {
                return GetSectionValue(SectionKey);
            }
            set
            {
                SetSectionValue(SectionKey, value);
            }
        }
        public string this[CONFIG_KEY confKey]
        {
            get
            {
                return GetSectionValue(confKey.ToString());
            }
            set
            {
                SetSectionValue(confKey.ToString(), value);
            }
        }
        private string GetSectionValue(string SectionKey)
        {
            DataRow dr = DtConfig.Rows.Find(SectionKey);
            if (dr != null)
            {
                return ShareMet.GetObjectString(dr["Value"]);
            }
            else
                return "";
        }
        private void SetSectionValue(string SectionKey, string Value)
        {
            DataRow dr = DtConfig.Rows.Find(SectionKey);
            if (dr == null)
            {
                DtConfig.Rows.Add(new object[] { SectionKey, Value, "" });
            }
            else
            {
                dr["Value"] = Value;
            }
        }
        public void Save()
        {
            try
            {
                if (_DS != null)
                {
                    _DS.WriteXml(_ConfigFile);
                    _DS.AcceptChanges();
                }
            }
            catch { }
        }
        private void CreateDataBase()
        {
            _DS = new DataSet("Config");
            DataTable dt = new DataTable("Section");
            dt.Columns.Add("SectionName", typeof(string)).AllowDBNull = false;
            dt.Columns.Add("Value", typeof(string)).AllowDBNull = true;
            dt.Columns.Add("Desc", typeof(string)).DefaultValue = "";
            dt.PrimaryKey = new DataColumn[] { dt.Columns["SectionName"] };
            _DS.Tables.Add(dt);
            try
            {
                if (ShareMet.FileExists(_ConfigFile))
                    _DS.ReadXml(_ConfigFile);
                else
                    _DS.WriteXml(_ConfigFile);
            }
            catch { }
        }
    }
}
